﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BlackBBS.Infrastructure.Repository;
using System.Xml.Serialization;

namespace BlackBBS.Models.Data
{
    [Serializable]
    public abstract class ModelBase : IRepositoryEntity
    {
        [NonSerialized]
        private Dictionary<string, string> _templateItems = null;

        public long Id { get; set; }

        [XmlIgnore]
        public Dictionary<string, string> TemplateItems
        {
            get
            {
                return this._templateItems;
            }
            set
            {
                this._templateItems = value;
            }
        }

        virtual internal void Init()
        {        
        }

        /// <summary>
        /// Take any template variables in the given input and substitute the current objects values using the values configured
        /// </summary>
        public string ProcessTemplate(string template)
        {
            var result = template;
            foreach (var kp in TemplateItems)
            {
                var value = GetType().GetProperty(kp.Value).GetValue(this, null); 
                string formatted = value == null ? string.Empty : value.ToString();
                result = result.Replace(kp.Key, formatted);
            }
            return result;
        }

    }
}
